<template>
  <el-dialog v-loading="loading" :title="showTitle" :visible.sync="dialogVisible" width="50%" @close="handleClose">
    <el-form ref="editFormRef" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="formData.name" style="width: 80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="formData.description" style="width: 80%" placeholder="1-50个字符" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="handleClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleDetail, getRoleList, addRole, editRole } from '@/api/role'
export default {
  data() {
    const checkRoleName = async (rule, value, callback) => {
      // 获取所有的角色列表数据
      const { rows } = await getRoleList({ page: 1, pagesize: 100000 })
      let isRepeat = true
      if (this.formData.id) {
        // 编辑：排除自己
        isRepeat = rows.some(item => item.name === value && item.id !== this.formData.id)
      } else {
        // 新增
        isRepeat = rows.some(item => item.name === value)
      }
      isRepeat ? callback(new Error('角色名称重复')) : callback()
    }
    return {
      loading: false,
      // 控制对话框的显示和隐藏
      dialogVisible: false,
      // 表单数据
      formData: {
        name: '',
        description: ''
      },
      // 验证规则
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' },
          { validator: checkRoleName, trigger: 'blur' }
        ],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑角色' : '添加角色'
    }
  },
  methods: {
    // 打开对话框
    async open(id) {
      if (id) {
        this.loading = true
        this.formData = await getRoleDetail(id)
        this.loading = false
      }
      this.dialogVisible = true
    },
    // 关闭对话框
    handleClose() {
      this.dialogVisible = false
      // 重置表单
      this.$refs.editFormRef.resetFields()
      // 重置数据
      /* this.formData = {
        id: '',
        name: '',
        description: ''
      } */
      Object.keys(this.formData).forEach(key => {
        this.formData[key] = ''
      })
    },
    // 提交
    btnOk() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return false
        try {
          // 验证通过
          if (this.formData.id) {
            await editRole(this.formData)
            this.$message.success('编辑成功')
          } else {
            await addRole(this.formData)
            this.$message.success('添加成功')
          }
          // 重新获取最近列表数据
          this.$emit('get-new-roles')
        } catch {
          // todo
        } finally {
          this.dialogVisible = false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
